package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.message.MessageListInDTO;
import com.qkl.xiatang.app.dto.message.MessageTypeSubDTO;
import com.qkl.xiatang.app.dto.message.MessageTypeSubWebDTO;
import com.qkl.xiatang.app.dto.pond.PondBackupWebDTO;
import com.qkl.xiatang.app.service.impl.MessageService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.MsgUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.applet.resources.MsgAppletViewer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;


    /**
     * 消息列表查询
     * @param params
     * @return
     */
    @GetMapping("/queryMessageList")
    public Result queryMessageList(@RequestParam Map<String,Object> params) {
        String pageIndex = StringUtils.trimToEmpty((String) params.get("pageIndex"));
        String pageSize = StringUtils.trimToEmpty((String) params.get("pageSize"));
        String messageTypeId = StringUtils.trimToEmpty((String) params.get("messageTypeId"));

        MessageListInDTO indto = new MessageListInDTO();
        indto.setPageIndex(AppUtils.parseInt(pageIndex));
        indto.setPageSize(AppUtils.parseInt(pageSize));
        indto.setMessageTypeId(messageTypeId);
        return Result.success(messageService.queryMessageList(indto),"消息列表查询");
    }

    /**
     * 消息详情
     * @param params
     * @return
     */
    @GetMapping("/queryMessageDetail")
    public Result queryMessageDetail(@RequestParam Map<String,Object> params) {
        String messageId = StringUtils.trimToEmpty((String) params.get("messageId"));
        if(StringUtils.isEmpty(messageId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toQueryResult(messageService.queryMessageDetail(messageId));
    }

    /**
     * 消息内容详情
     * @param params
     * @return
     */
    @GetMapping("/queryMessageContentDetail")
    public Result queryMessageContentDetail(@RequestParam Map<String,Object> params) {
        String messageId = StringUtils.trimToEmpty((String) params.get("messageId"));
        if(StringUtils.isEmpty(messageId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toQueryResult(messageService.queryMessageContentDetail(messageId));
    }

    /**
     * 查询未读消息
     * @return
     */
    @GetMapping("/queryUnreadMessageTotal")
    public Result queryUnreadMessageTotal() {
        return ControllerHelper.toQueryResult(messageService.queryUnreadMessageTotal());
    }

    /**
     * 消息类型查询
     * @return
     */
    @GetMapping("/queryMessageTypeList")
    public Result queryMessageTypeList() {
        return ControllerHelper.toQueryResult(messageService.queryMessageTypeList());
    }

    /**
     * 拒收消息
     * @param params
     * @return
     */
    @GetMapping("/refuseMessage")
    public Result refuseMessage(@RequestParam Map<String,String> params) {
        String subType = StringUtils.trimToEmpty(params.get("subType"));
        log.info("=====refuseMessage.subType={}",subType);
        MessageTypeSubDTO subDTO = new MessageTypeSubDTO();
        subDTO.setStatus(MsgUtils.SUB_TYPE_RECEIVE_NOT);
        subDTO.setSubType(AppUtils.parseInt(subType));
        MessageTypeSubWebDTO  webDTO = new MessageTypeSubWebDTO();
        webDTO.setSubTypeList(new ArrayList<>());
        webDTO.getSubTypeList().add(subDTO);
        return ControllerHelper.toOperateResult(messageService.messageReceiveSet(webDTO));
    }

    /**
     * 消息子类型查询
     * @return
     */
    @GetMapping("/queryMessageSubType")
    public Result queryMessageSubType() {
        return ControllerHelper.toQueryResult(messageService.queryMessageSubType());
    }

    /**
     * 消息接收设置
     * @param params
     * @return
     */
    @PostMapping("/messageReceiveSet")
    public Result messageReceiveSet(@RequestBody Map<String,Object> params) {
        Integer subType = (Integer)params.get("subType");
        Integer status = (Integer)params.get("status");

        MessageTypeSubWebDTO  webDTO = new MessageTypeSubWebDTO();
        webDTO.setSubTypeList(new ArrayList<>());
        webDTO.getSubTypeList().add(MessageTypeSubDTO.create(subType,status));

        return ControllerHelper.toOperateResult(messageService.messageReceiveSet(webDTO));
    }

}
